/*
 * @Author: lll 1280388921@qq.com
 * @Date: 2025-08-11 19:02:45
 * @LastEditors: lll 1280388921@qq.com
 * @LastEditTime: 2025-08-13 15:51:38
 * @FilePath: /lesson43/ServerCal.hpp
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
#pragma once
#include <iostream>
#include "Protocol.hpp"

enum
{
    Div_Zero = 1,
    Mod_Zero,
    Other_Oper
};

class ServerCal
{
public:
    ServerCal() {}
    Response CalculatorHelper(Request &req)
    {
        Response res(0, 0);
        switch (req.op)
        {
        case '+':
            res.result = req.x + req.y;
            break;
        case '-':
            res.result = req.x - req.y;
            break;
        case '*':
            res.result = req.x * req.y;
            break;
        case '/':
        {
            if (req.y == 0)
                res.code = Div_Zero;
            else
                res.result = req.x / req.y;
        }
        break;
        case '%':
        {
            if (req.y == 0)
                res.code = Mod_Zero;
            else
                res.result = req.x % req.y;
        }
        break;
        default:
            res.code=Other_Oper;
            break;
        }
        return res;
    }
    string Calculator(string &pakg)
    {
        string text;
        bool r = Decode(pakg, &text);
        if (!r)
            return "";

        Request req;
        r = req.Deserialize(text);
        if (!r)
            return "";

        text="";
        Response res = CalculatorHelper(req);
        res.Serialize(&text);
        text = Encode(text);

        return text;
    }
    ~ServerCal() {}
};
